const util = require("../../../utils/util");
import Toast from "../../../vant/weapp/toast/toast";
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    store_name: '',
    type: 1,
    store: [],
    orderlist: [],
    is_user_show: true,
    is_user: 0,
    showUser: false,
    show: false,
    orderday: 0,
    vipday: 0,
    couponday: 0,
    orderdaysum: 0
  },

  onLoad(options) {
    const that = this
    console.log(options)
    if (options.store_id) {
      that.setData({
        store_id: options.store_id
      })
      that.getstore()
      that.getroomlist(options.store_id)
      return
    }
  },
  onPullDownRefresh: function () {
    this.onRefresh();
  },
  onRefresh: function () {
    //导航条加载动画
    wx.showNavigationBarLoading()
    //loading 提示框
    wx.showLoading({
      title: 'Loading...',
    })

    this.onShow();
    setTimeout(function () {
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000)
  },
  typeTab(e) {
    this.setData({
      type: e.currentTarget.dataset.num,
    })
  },

  onShow: function () {
    const that = this
    that.getroomlist(that.data.store_id)
  },

  //   登录
  login() {
    console.log('login')
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    }
  },
  // 登录遮罩层
  close(e) {
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    console.log(e)
    this.setData({
      showUser: true,
    });
    wx.setStorageSync('userInfo', e.detail)
  },
  // 门店列表
  getstore() {
    const that = this
    app.http.get(app.api.adminstore, {}, (res) => {
      that.setData({
        store: res.data.lists
      })
      if (that.data.store_id == 0) {
        let store_id = that.data.store[that.data.index].store_id.toString()
        that.setData({
          store_id,
          imgUrls: that.data.store[that.data.index].head_url
        })

        that.getroomlist(store_id)
      } else {
        let stores = that.data.store;
        for (let i in stores) {
          if (stores[i].store_id == that.data.store_id) {
            that.setData({
              index: i,
              imgUrls: stores[i].head_url
            })
            return
          }
        }
      }
    })
  },
  // 包厢列表
  getroomlist(store_id) {
    const that = this

    //包厢列表
    app.http.get(app.api.roomstorelist, {
      store_id
    }, (res) => {
      that.setData({
        privatestore: res.data.lists,
        orderlist: res.data.orderlist,
        orderday: res.data.orderday,
        vipday: res.data.vipday,
        couponday: res.data.couponday,
        orderdaysum: res.data.orderdaysum,
        isList: false
      })
    })

  },


  // 切换门店
  bindPickerChange(e) {
    const that = this
    that.setData({
      chooseIndex: -1,
      index: e.detail.value,
      store_id: that.data.store[e.detail.value].store_id,
      imgUrls: that.data.store[e.detail.value].head_url
    })
    that.getroomlist(that.data.store[e.detail.value].store_id)
    // that.getPackage()
    that.getstore()
  },

  // 门禁指令
  doorcommand(e) {

    var doorId = e.currentTarget.dataset.doorId;
    var doorType = e.currentTarget.dataset.doorType;
    var status = e.currentTarget.dataset.status;
    var content = status == 1 ? '确认发送开门指令吗？' : '确认发送断电指令吗？'
    wx.showModal({
      title: '提示',
      content: content,
      success: function (res) {
        if (res.confirm) {
          app.http.post(app.api.doorcommand, {
            doorId,
            doorType,
            status
          }, res => {
            setTimeout(function () {
              wx.hideLoading()
            }, 0)
            if (res.code === 200) {
              if (status == 1) {
                wx.showToast({
                  title: '开门成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '断电成功',
                  icon: 'success',
                  duration: 2000
                });
              }

            } else {
              Toast.fail(res.message)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消');
          // 执行取消后的操作
        }
      }
    });



  },

  ordercommand(e) {
    const that = this
    var orderOn = e.currentTarget.dataset.orderOn;
    var status = e.currentTarget.dataset.status;
    var content = status == 2 ? '确认提前结束订单，不退款吗？' : '确认结束订单，并退款吗？'
    wx.showModal({
      title: '提示',
      content: content,
      success: function (res) {
        if (res.confirm) {
          app.http.post(app.api.ordercommand, {
            orderOn,
            status
          }, res => {
            setTimeout(function () {
              wx.hideLoading()
            }, 0)
            if (res.code === 200) {
              wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 2000
              });
              that.getroomlist(that.data.store_id)
            } else {
              Toast.fail(res.msg)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消');
          // 执行取消后的操作
        }
      }
    });




  }
});